﻿@namespace MudBlazor
@typeparam T
@inherits MudBaseInput<T>

<CascadingValue Name="SubscribeToParentForm" Value="false" IsFixed="true">
    <div class="@OuterClassname" id="@_elementId" @onfocusout="@OnFocusOutAsync">
        <MudInputControl Label="@Label"
                         Variant="@Variant"
                         HelperId="@GetHelperId()"
                         HelperText="@HelperText"
                         HelperTextOnFocus="@HelperTextOnFocus"
                         FullWidth="@FullWidth"
                         Margin="@Margin"
                         Class="@Classname"
                         Style="@Style"
                         Error="@HasErrors"
                         ErrorId="@ErrorId"
                         ErrorText="@GetErrorText()"
                         Disabled="@GetDisabledState()"
                         Required="@Required"
                         ForId="@InputElementId"
                         @onpointerdown="@ToggleMenu">
            <InputContent>
                <MudInput @ref="_elementReference"
                          InputType="@(CanRenderValue || (Strict && !IsValueInList) ? InputType.Hidden : InputType.Text)"
                          Class="@InputClassname"
                          Margin="@Margin"
                          Placeholder="@Placeholder"
                          Label="@Label"
                          Variant="@Variant"
                          Typo="@Typo"
                          TextUpdateSuppression="false"
                          Value="@(Strict && !IsValueInList ? null : Text)"
                          Underline="@Underline"
                          Disabled="@GetDisabledState()"
                          ReadOnly="true"
                          Error="@Error"
                          ErrorId="@ErrorId"
                          HelperId="@GetHelperId()"
                          HelperText="@HelperText"
                          AdornmentText="@AdornmentText"
                          AdornmentIcon="@_currentIcon"
                          Adornment="@Adornment"
                          AdornmentColor="@AdornmentColor"
                          IconSize="@IconSize"
                          AdornmentAriaLabel="@AdornmentAriaLabel"
                          OnAdornmentClick="@OnAdornmentClick"
                          Clearable="@(Clearable && !GetReadOnlyState())"
                          ClearIcon="@ClearIcon"
                          OnClearButtonClick="@(async (e) => await SelectClearButtonClickHandlerAsync(e))"
                          @attributes="UserAttributes"
                          OnBlur="@OnBlurAsync"
                          ShrinkLabel="@ShrinkLabel"
                          InputId="@InputElementId"
                          Required="@Required">
                    @if (CanRenderValue) {
                        @GetSelectedValuePresenter()
                    }
                </MudInput>
                <MudPopover Open=@(_open)
                            MaxHeight="@MaxHeight"
                            AnchorOrigin="@AnchorOrigin"
                            TransformOrigin="@TransformOrigin"
                            OverflowBehavior="@DropdownSettings.OverflowBehavior"
                            Class="@PopoverClass"
                            Fixed="@DropdownSettings.Fixed"
                            RelativeWidth="@FullWidth">
                    <CascadingValue Value="@((IMudSelect)this)" IsFixed="true">
                        <MudList T="string"
                                 Class="@ListClass"
                                 Dense="@Dense"
                                 @bind-SelectedValue="_activeItemId">
                            @if (MultiSelection && SelectAll) {
                                <MudListItem Value="@("__SelectAll__")"
                                             Icon="@SelectAllCheckBoxIcon"
                                             Text="@SelectAllText"
                                             OnClick="SelectAllClickAsync"
                                             OnClickPreventDefault="true"
                                             Dense="@Dense"
                                             Class="mb-2" />
                                <MudDivider />
                            }
                            @ChildContent
                        </MudList>
                    </CascadingValue>
                </MudPopover>
            </InputContent>
        </MudInputControl>
    </div>
    @*Shadow select items for IsValueInList and CanRenderValue*@
    <CascadingValue Value="@((IMudShadowSelect)this)" IsFixed="true">
        <CascadingValue Name="HideContent" Value="true">
            @ChildContent
        </CascadingValue>
    </CascadingValue>

</CascadingValue>
<!-- pointerdown instead of click needed to close the menu before OnLostFocus runs -->
<MudOverlay Visible="_open" @onpointerdown="@(() => CloseMenu(false))" LockScroll="@LockScroll" />
